<?php

class StatusDao extends Dao {

    function __construct() {
        parent::__construct();
        $this->table = 'status';
    }
    
    public function insertObject($obj){
            $result = DB::insert(DB::raw("INSERT INTO status (id, name, description, created_at) ".
                "VALUES ('".$obj->getId()."' ".
                         ", '".$obj->getName()."' ".
                        ", '".$obj->getDescription()."' ".
                        ", null ".
                        ")"));
            
            if (!$result) { return null; }

            return $obj->getId();
    }

    public function updateObject($obj, $old_id){
       DB::select(DB::raw("UPDATE status SET "
               . "id = '".$obj->getId()."', "
               . "name = '".$obj->getName()."', "
               . "description = '".$obj->getDescription()."', "
               . "updated_at = null "
               . "Where id='".$old_id."'"));

       return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM status where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM status where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM status"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }
    
    public function getListCount($criteria=null) {
        return parent::getListCount($criteria);
    }
    
    public function toObject($rowdata=null){
        $obj = new Status();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setDescription($rowdata->description);
        $obj->setIsLoaded(true);
        
        return $obj;
    }
}
